package com.simpleaccount.Common;

import android.content.Context;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.Entity.Category;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.R;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/7/17.
 */

public class Utils {
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
    public static void setImage(ImageView view, String category){

        if(null==category){
            view.setImageResource(R.drawable.new_small_point);
            view.setBackgroundResource(R.drawable.new_small_point);
        }
        else if(category.equals("餐饮")){
            view.setImageResource(R.drawable.pic_food);
            view.setBackgroundResource(R.drawable.bg_food);
        }
        else if(category.equals("医疗")){
            view.setImageResource(R.drawable.pic_medical);
            view.setBackgroundResource(R.drawable.bg_medical);
        }
        else if(category.equals("贵重品")){
            view.setImageResource(R.drawable.pic_valuables);
            view.setBackgroundResource(R.drawable.bg_valuables);
        }
        else if(category.equals("娱乐")){
            view.setImageResource(R.drawable.pic_entertainment);
            view.setBackgroundResource(R.drawable.bg_entertainment);
        }
        else if(category.equals("学习")){
            view.setImageResource(R.drawable.pic_study);
            view.setBackgroundResource(R.drawable.bg_study);
        }//5
        else if(category.equals("维修")){
            view.setImageResource(R.drawable.pic_repair);
            view.setBackgroundResource(R.drawable.bg_repair);
        }
        else if(category.equals("服装")){
            view.setImageResource(R.drawable.pic_clothes);
            view.setBackgroundResource(R.drawable.bg_clothes);
        }
        else if(category.equals("购物")){
            view.setImageResource(R.drawable.pic_shopping);
            view.setBackgroundResource(R.drawable.bg_shopping);
        }
        else if(category.equals("人情")){
            view.setImageResource(R.drawable.pic_relation);
            view.setBackgroundResource(R.drawable.bg_relation);
        }
        else if(category.equals("一般")){
            view.setImageResource(R.drawable.pic_general);
            view.setBackgroundResource(R.drawable.bg_general);
        }//10
        else if(category.equals("出行")){
            view.setImageResource(R.drawable.pic_transport);
            view.setBackgroundResource(R.drawable.bg_transport);
        }
        else if(category.equals("旅游")){
            view.setImageResource(R.drawable.pic_travel);
            view.setBackgroundResource(R.drawable.bg_travel);
        }
        else if(category.equals("住宿")){
            view.setImageResource(R.drawable.pic_hotel);
            view.setBackgroundResource(R.drawable.bg_hotel);
        }
        else if(category.equals("其他")){
            view.setImageResource(R.drawable.pic_more);
            view.setBackgroundResource(R.drawable.bg_more);
        }
        else if(category.equals("还贷")){
            view.setImageResource(R.drawable.pic_loan);
            view.setBackgroundResource(R.drawable.bg_loan);
        }//15

        //income
        else if(category.equals("工资")){
            view.setImageResource(R.drawable.pic_salary);
            view.setBackgroundResource(R.drawable.bg_salary);
        }
        else if(category.equals("兼职")){
            view.setImageResource(R.drawable.pic_part_time_job);
            view.setBackgroundResource(R.drawable.bg_part_time_job);
        }
        else if(category.equals("红包")){
            view.setImageResource(R.drawable.pic_red_envelope);
            view.setBackgroundResource(R.drawable.bg_red_envelope);
        }
        else if(category.equals("普通")){
            view.setImageResource(R.drawable.pic_common);
            view.setBackgroundResource(R.drawable.bg_common);
        }
        else if(category.equals("生活费")){
            view.setImageResource(R.drawable.pic_pocket_money);
            view.setBackgroundResource(R.drawable.bg_pocket_money);
        }
        else if(category.equals("奖金")){
            view.setImageResource(R.drawable.pic_prize);
            view.setBackgroundResource(R.drawable.bg_prize);
        }

    }
    public static HashMap<String,Float> getMoneyByCategory(List<Entry> entries){
        HashMap<String,Float> moneys=new HashMap<>();
        for(Entry entry:entries){
            String cate = entry.getCategory();
            Float sum =moneys.get(cate);
            moneys.put(cate,(float)entry.getMoney()+(null==sum?0:sum));
        }
        return moneys;
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        //获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); //计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); //统计所有子项的总高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        //listView.getDividerHeight()获取子项间分隔符占用的高度
        //params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
